# Adogs
# 时间: 2021/7/19 15:16
import pygame
from actor import WalkDir, WalkByImage, ShootDir
from actor.shoot_soldier import ShootSoldier


class XiaoTie(pygame.sprite.Sprite):
    """
    小铁类
    方法：
    1.__init__(x, y)
    2.run(down_flag, key_list, obstacle_group, japanese_group, red_army_group)
    3.set_status(status)
    4.draw(surface, x, y)
    5.set_pos(x, y)
    """
    # 小铁基本参数
    hp = 100
    name = '小铁'
    pos_x = 1000
    pos_y = 200
    image_wide = 49
    image_high = 74
    current_status = 'boy'

    # 小铁的三种状态
    boy_base_action = None
    army_base_action = None
    army_shoot_action = None

    dir = WalkDir.down

    def __init__(self, x, y):
        super(XiaoTie, self).__init__()
        self.run_flag = False
        self.boy_base_action = WalkByImage("resources/picture/xiao_tie/man-4.png", self.image_wide, self.image_high,
                                           True)
        self.army_base_action = WalkByImage("resources/picture/chinese/red_army/boy-1.png", self.image_wide,
                                            self.image_high, True)
        self.army_shoot_action = ShootSoldier("resources/picture/chinese/red_army/red_army1.png",
                                              "resources/picture/chinese/red_army/red_army1-1.png", x, y, "小铁", 100)
        self.shoot_sound = pygame.mixer.Sound("resources/sound/枪声.mp3")
        self.shoot_sound.set_volume(0.2)
        self.boy_base_action.set_dir(self.dir)
        self.army_base_action.set_dir(self.dir)
        self.army_shoot_action.walk_action.set_dir(self.dir)

        self.current_action = self.boy_base_action  # 初始化定义最开始的状态
        self.pos_x = x
        self.pos_y = y
        self.rect = pygame.Rect(self.pos_x + 19, self.pos_y + 60, 20, 10)

    def run(self, down_flag, key_list, obstacle_group, japanese_group, red_army_group):
        """
        小铁运动函数
        :param down_flag: bool型，判断是否点击按键
        :param key_list:  列表型，储存键盘上每个键的bool值，判断是否有键按下
        :param obstacle_group: 障碍物组
        :param japanese_group: 日本人组
        :param red_army_group: 红军组
        :return: 无返回值
        """
        # 通过wasd来控制人物的上下左右移动
        off_x = 0
        off_y = 0
        if key_list[pygame.K_s]:
            self.army_shoot_action.shoot_action.set_dir(ShootDir.not_shoot)
            self.dir = WalkDir.down
            off_y += 10
            self.run_flag = True
        elif key_list[pygame.K_w]:
            self.army_shoot_action.shoot_action.set_dir(ShootDir.not_shoot)
            self.dir = WalkDir.on
            off_y += -10
            self.run_flag = True
        elif key_list[pygame.K_a]:
            self.army_shoot_action.shoot_action.set_dir(ShootDir.not_shoot)
            self.dir = WalkDir.left
            off_x += -10
            self.run_flag = True
        elif key_list[pygame.K_d]:
            self.army_shoot_action.shoot_action.set_dir(ShootDir.not_shoot)
            self.dir = WalkDir.right
            off_x += 10
            self.run_flag = True

        # 通过点击按键uiojkl来控制人物的射击
        if down_flag:
            if key_list[pygame.K_u]:
                if self.army_shoot_action.shoot_action.shoot_dir == ShootDir.not_shoot:
                    self.army_shoot_action.shoot_action.set_dir(ShootDir.left_up)
                else:
                    self.army_shoot_action.shoot_action.set_dir(ShootDir.not_shoot)
                    self.dir = WalkDir.left

            elif key_list[pygame.K_o]:
                if self.army_shoot_action.shoot_action.shoot_dir == ShootDir.not_shoot:
                    self.army_shoot_action.shoot_action.set_dir(ShootDir.right_up)
                else:
                    self.army_shoot_action.shoot_action.set_dir(ShootDir.not_shoot)
                    self.dir = WalkDir.right

            elif key_list[pygame.K_j]:
                if self.army_shoot_action.shoot_action.shoot_dir == ShootDir.not_shoot:
                    self.army_shoot_action.shoot_action.set_dir(ShootDir.left_down)
                else:
                    self.army_shoot_action.shoot_action.set_dir(ShootDir.not_shoot)
                    self.dir = WalkDir.left

            elif key_list[pygame.K_l]:
                if self.army_shoot_action.shoot_action.shoot_dir == ShootDir.not_shoot:
                    self.army_shoot_action.shoot_action.set_dir(ShootDir.right_down)
                else:
                    self.army_shoot_action.shoot_action.set_dir(ShootDir.not_shoot)
                    self.dir = WalkDir.right

            elif key_list[pygame.K_i]:
                if self.army_shoot_action.shoot_action.shoot_dir == ShootDir.not_shoot:
                    self.army_shoot_action.shoot_action.set_dir(ShootDir.up)
                else:
                    self.army_shoot_action.shoot_action.set_dir(ShootDir.not_shoot)
                    self.dir = WalkDir.on

            elif key_list[pygame.K_k]:
                if self.army_shoot_action.shoot_action.shoot_dir == ShootDir.not_shoot:
                    self.army_shoot_action.shoot_action.set_dir(ShootDir.down)
                else:
                    self.army_shoot_action.shoot_action.set_dir(ShootDir.not_shoot)
                    self.dir = WalkDir.down

        # 设置三个状态的人物方向
        self.boy_base_action.set_dir(self.dir)
        self.army_base_action.set_dir(self.dir)
        self.army_shoot_action.walk_action.set_dir(self.dir)

        # 暂时存储原来的位置坐标，如果遇到障碍物就不进行位置变换
        temp_rect = self.rect
        self.rect = self.rect.move(off_x, off_y)
        collide_list = pygame.sprite.spritecollide(self, obstacle_group, False)
        if len(collide_list) > 0:
            self.rect = temp_rect
        elif len(pygame.sprite.spritecollide(self, japanese_group, False)) > 0:
            self.rect = temp_rect
        elif len(pygame.sprite.spritecollide(self, red_army_group, False)) > 0:
            self.rect = temp_rect
        else:
            self.pos_x += off_x
            self.pos_y += off_y

    def set_status(self, status: str):
        """
        设置小铁的当前状态
        :param status: 小铁的状态（boy，shoot）
        :return:
        """
        if status == "boy":
            self.current_action = self.boy_base_action
        elif status == "shoot":
            self.current_action = self.army_shoot_action
            self.rect = pygame.Rect(self.pos_x + 19, self.pos_y + 80, 20, 10)
        else:
            self.current_action = self.army_base_action

    def draw(self, surface: pygame.surface, x: int, y: int):
        """
        小铁绘制函数
        :param surface: 绘制屏幕
        :param x: 屏幕相对x坐标
        :param y: 屏幕相对y坐标
        :return: 无返回值
        """
        # 小铁不是射击状态时的绘制函数
        if self.current_action != self.army_shoot_action or \
                self.army_shoot_action.shoot_action.shoot_dir == ShootDir.not_shoot:

            self.shoot_sound.stop()

            if self.current_action != self.army_shoot_action:
                current_image = self.current_action.get_image(self.run_flag)
            else:
                current_image = self.army_shoot_action.walk_action.get_image(self.run_flag)
            self.run_flag = False
            surface.blit(current_image, (self.pos_x - x, self.pos_y - y))
            # pygame.draw.rect(surface, pygame.Color(255, 255, 0), (self.pos_x - x + 19, self.pos_y - y + 60, 20,
            # 10), 1)

        # 小铁是射击状态时的绘制函数
        else:
            self.shoot_sound.play(-1)   # 无限循环播放枪声

            if self.army_shoot_action.shoot_action.shoot_dir == ShootDir.down:
                self.army_shoot_action.walk_action.set_dir(WalkDir.down)
                current_img = self.army_shoot_action.walk_action.get_image(True)
                surface.blit(current_img, (self.pos_x - x, self.pos_y - y))
                current_img = self.army_shoot_action.shoot_action.get_image()
                surface.blit(current_img, (self.pos_x - 30 - x, self.pos_y - 5 - y))
            elif self.army_shoot_action.shoot_action.shoot_dir == ShootDir.up:
                self.army_shoot_action.walk_action.set_dir(WalkDir.on)
                current_img = self.army_shoot_action.shoot_action.get_image()
                surface.blit(current_img, (self.pos_x - x, self.pos_y - y))
                current_img = self.army_shoot_action.walk_action.get_image(True)
                surface.blit(current_img, (self.pos_x - x, self.pos_y - y))
            else:
                current_img = self.army_shoot_action.shoot_action.get_image()
                surface.blit(current_img, (self.pos_x - x, self.pos_y - y))

    def set_pos(self, x, y):
        """
        设置小铁的位置
        :param x:
        :param y:
        :return:
        """
        self.pos_x = x
        self.pos_y = y
        self.rect = pygame.Rect(self.pos_x + 19, self.pos_y + 60, 20, 10)
